package com.celan.year2023.month02.day10;

public class Solution {
    static final int MOD = 1000000007;

    public int dieSimulator(int n, int[] rollMax) {
        //dp[i][j][k]:
        // i->投掷筛子i次
        // j->上一次投掷筛子的点数
        // k->上一次连续投掷的数量
        int[][][] dp = new int[n + 1][6][16];
        //初始化：第一次投色子
        for (int j = 0; j < 6; j++) {
            dp[1][j][1] = 1;
        }
        //若此次投色子点数不同：dp[i][p][1]+dp[i-1][j][k]
        //若此次投色子点数相同，并且k+1<=rollMax[j]：dp[i][p][k+1]+dp[i-1][j][k]
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j <= 5; j++) {
                for (int k = 1; k <= rollMax[j]; k++) {
                    for (int p = 0; p < 6; p++) {
                        if (p != j) {
                            dp[i][p][1] = (dp[i][p][1] + dp[i - 1][j][k]) % MOD;
                        } else if (k + 1 <= rollMax[j]) {
                            dp[i][p][k + 1] = (dp[i][p][k + 1] + dp[i - 1][j][k]) % MOD;
                        }
                    }
                }
            }
        }
        //将所有可能的结果相加
        int res = 0;
        for (int j = 0; j < 6; j++) {
            for (int k = 1; k <= rollMax[j]; k++) {
                res = (res + dp[n][j][k]) % MOD;
            }
        }
        return res;
    }
}
